package com.wsx.rpc.metrics.gauges;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;

import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author wangshuaixin
 * @Title: com.wsx.rpc.metrics.gauges.GaugesTest
 * @Description: Gauges是一个最简单的计量，一般用来统计瞬时状态的数据信息
 * @date 2018/11/23
 */
public class GaugesTest {

    /**
     * 实例化一个registry，最核心的一个模块，相当于一个应用程序的metrics系统的容器，维护一个Map
     */
    private static final MetricRegistry METRIC_REGISTRY = new MetricRegistry();

    private static Queue<String> queue = new LinkedBlockingQueue<>();

    /**
     * 注册一个输出通道，控制台输出
     */
    private static ConsoleReporter reporter = ConsoleReporter.forRegistry(METRIC_REGISTRY).build();


    public static void main(String[] args) {

        reporter.start(3, TimeUnit.SECONDS);

        Gauge<Integer> gauge = new Gauge<Integer>() {
            @Override
            public Integer getValue() {
                return queue.size();
            }
        };

        METRIC_REGISTRY.register(MetricRegistry.name(GaugesTest.class, "quene", "size"), gauge);


        for (int i = 0; i < 10; i++) {
            queue.add(String.valueOf(i));
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
